package com.imufe.chemicalenterprisedangerwarning.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestParam;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

/**
 * Swagger-ui的配置类
 *
 * @author BaiBan
 * @since 2022/7/21
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket defaultApi() {
        return new Docket(DocumentationType.OAS_30)
                .useDefaultResponseMessages(false) // 去除默认返回结果
                .apiInfo(apiInfo()).enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.imufe.chemicalenterprisedangerwarning.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(this.getParameters());
    }

    // 团队信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("化工企业危险预警系统-API文档")
                .contact(new Contact("化工企业危险预警系统", "这是网址", "2275077113@qq.com"))
                .description("大四课程的项目")
                .build();
    }

    private List<RequestParameter> getParameters() {

        RequestParameterBuilder builder = new RequestParameterBuilder();
        RequestParameter parameter = builder
                .name("Token")
                .description("登录令牌")
                .in(ParameterType.HEADER)
                .required(false)
                .build();

        List<RequestParameter> aParameters = new ArrayList<>();
        aParameters.add(parameter);
        return aParameters;
    }
}